import axios from 'axios'
import qs from 'qs'

// axios 配置
axios.defaults.timeout = 30000
axios.defaults.baseURL = 'http://qc.chinacpc.org'

// POST传参序列化
axios.interceptors.request.use((config) => {
    if (config.method  === 'post') {
        config.data = qs.stringify(config.data)
    }
    return config
}, (error) => {
    return Promise.reject(error)
})

// 返回状态判断
axios.interceptors.response.use((res) => {
    return res
}, (error) => {
    return Promise.reject(error)
})

export function fetch (url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
        .then(function (response) {
            resolve(response.data)
        })
        .catch((error) => {
            // console.log(error)
        })
    })
}

export function post (url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(function (response) {
                resolve(response.data)
            })
            .catch((error) => {
                // console.log(error)
            })
    })
}

export default {
    getAvg(core, hierarchy, start, end) {
        return fetch('/areaqc/get_avg', {
            'core': core,
            'hierarchy': hierarchy,
            'start': start,
            'end': end
        })
    },
    getRank(core, start, end) {
         return fetch('/areaqc/get_rank', {
            'core': core,
            'province': '江苏',
            'start': start,
            'end': end
         })
    }

}
